import { login } from "@/http/user.js"
export let user = {
    namespaced:true,
    state:{
        token:localStorage.getItem("houtaitoken"),
        get username() {
            return localStorage.getItem("houtaiusername");
        },
        set username(v) {
            if(v) {
                localStorage.setItem("houtaiusername",v)
            }else{
                localStorage.removeItem("houtaiusername")
            }
        },
    },
    actions:{
        async loginFn({ commit }, obj) {
            let res = await login(obj)
            if(res.meta.status == 200) {
                commit("SET_TOKEN",res.data.token)
                commit("SET_USERNAME",res.data.username)
                return Promise.resolve()
            }else{
                return Promise.reject()
            }
        }
    },
    mutations:{
        SET_TOKEN(state,token) {
            state.token = token;
            localStorage.setItem("houtaitoken",token)
        },
        SET_USERNAME(state,username) {
            state.username = username;
        },
        DEL_TOKEN(state) {
            localStorage.removeItem("houtaitoken");
        },
        DEL_USERNAME(state) {
            state.username= ""
        }
    },
}